package com.anolesoft.yr.mbus.contm.service.impl;

import com.anolesoft.yr.mbus.contm.dao.ContmContentDao;
import com.anolesoft.yr.mbus.contm.dao.ContmMainDao;
import com.anolesoft.yr.mbus.contm.entity.ContmContent;
import com.anolesoft.yr.mbus.contm.entity.Paragraph;
import com.anolesoft.yr.mbus.contm.service.ContmCreateService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContmCreateServiceImpl implements ContmCreateService {
    @Autowired
    private ContmMainDao contmMainDao;

    @Autowired
    private ContmContentDao contmContentDao;


    @Override

    public void addContm(String contmMainJson){
      /*  JSONObject jsonObject = JSONObject.parseObject(contmMainJson);
        ContmMain contmMain = new ContmMain(new  ContmMainDto(jsonObject));
        contmMainDao.insert(contmMain);
        contmMain.getContmContents().forEach(a->{
            contmMContentInsert(a);

        });*/
    }

    public void contmMContentInsert(ContmContent contmContent){
        contmContentDao.insert(contmContent);
      /*  contmContent.getContmContentsTypeOfTitle().forEach(a->{
            contmMContentInsert(a);
        });
        contmContent.getContmContentsTypeOfContent().forEach(a->{
            contmMContentInsert(a);
        });*/
    }



    /**
     * 根据获取的html格式数据进行分段解析
     * @param paragraphs
     * @return
     * @throws DocumentException
     */

    @Override

    public List<Paragraph> getParagraphXml(String paragraphs) throws DocumentException {
        List<Paragraph> paragraphList = new ArrayList();

        String html = "<html>" + paragraphs + "</html>";
        Document document = DocumentHelper.parseText(html);
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        String text = rootElement.getText();
        if (!"".equals(text)) {
            int i = 0;
            while (true) {
                int index = paragraphs.indexOf(text, i);
                if ("<br/>".equals(paragraphs.substring(index + text.length(), index + text.length() + 5))) {
                    html = "<html>" + paragraphs.substring(0, index) + "<p>" + paragraphs.substring(index,index+ text.length()) + "</p>" + paragraphs.substring(index + text.length() + 5) + "</html>";
                    break;
                } else {
                    i++;
                }
            }
            document = DocumentHelper.parseText(html);
            rootElement = document.getRootElement();
            elements = rootElement.elements();
        }
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            if ("p".equals(element.getName())) {
                paragraphList.add(Paragraph.builder().orderCode(i * 1000000).type("p").xml(element.asXML()).build());
            } else {
                if ("ol".equals(element.getName())) {
                    List<Element> liElements = element.elements();
                    for (int j = 0; j < liElements.size(); j++) {
                        paragraphList.add(Paragraph.builder().orderCode(i * 1000000 + j).type("ol").xml(liElements.get(j).asXML()).build());
                    }
                } else if ("ul".equals(element.getName())) {
                    List<Element> liElements = element.elements();
                    for (int j = 0; j < liElements.size(); j++) {
                        paragraphList.add(Paragraph.builder().orderCode(i * 1000000 + j).type("ul").xml(liElements.get(j).asXML()).build());
                    }
                }else{
                    paragraphList.add(Paragraph.builder().orderCode(i * 1000000).type("p").xml(element.asXML()).build());
                }
            }
        }
        paragraphList=paragraphList.stream().sorted(Comparator.comparingInt(Paragraph::getOrderCode)).collect(Collectors.toList());
        for (int i = 0; i < paragraphList.size(); i++) {
            paragraphList.get(i).setOrderCode(i);
        }
        return paragraphList;
    }


}
